import 'package:flutter/material.dart';
import 'home_page.dart';
import 'profile_page.dart';
import 'add_collection_page.dart';
import 'discover_page.dart';
import 'message_page.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _currentIndex = 0;
  final GlobalKey<HomePageState> _homePageKey = GlobalKey<HomePageState>();

  List<Widget> get _pages => [
        HomePage(key: _homePageKey),
        const DiscoverPage(),
        const MessagePage(),
        const ProfilePage(),
      ];

  void _onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  void _onAddTapped() async {
    final result = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const AddCollectionPage(),
      ),
    );

    // 如果添加成功，切换到首页并刷新
    if (result == true && mounted) {
      setState(() {
        _currentIndex = 0; // 切换到首页
      });
      // 调用首页的刷新方法
      _homePageKey.currentState?.loadCollections();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: Container(
        height: 72,
        decoration: BoxDecoration(
          color: Theme.of(context).bottomAppBarTheme.color ??
                 Theme.of(context).colorScheme.surface,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.1),
              blurRadius: 4,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: Transform.translate(
          offset: const Offset(0, -12),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: InkWell(
                  onTap: () => _onTabTapped(0),
                  child: Center(
                    child: Text(
                      '首页',
                      style: TextStyle(
                        fontSize: 16,
                        color: _currentIndex == 0
                            ? Colors.black
                            : Colors.grey,
                        fontWeight: _currentIndex == 0
                            ? FontWeight.bold
                            : FontWeight.normal,
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: InkWell(
                  onTap: () => _onTabTapped(1),
                  child: Center(
                    child: Text(
                      '发现',
                      style: TextStyle(
                        fontSize: 16,
                        color: _currentIndex == 1
                            ? Colors.black
                            : Colors.grey,
                        fontWeight: _currentIndex == 1
                            ? FontWeight.bold
                            : FontWeight.normal,
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: InkWell(
                  onTap: _onAddTapped,
                  child: Center(
                    child: Container(
                      width: 44,
                      height: 36,
                      decoration: BoxDecoration(
                        color: Theme.of(context).colorScheme.primary,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Center(
                        child: Transform.translate(
                          offset: const Offset(0, -1),
                          child: const Text(
                            '+',
                            style: TextStyle(
                              fontSize: 32,
                              color: Colors.white,
                              fontWeight: FontWeight.w300,
                              height: 1.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: InkWell(
                  onTap: () => _onTabTapped(2),
                  child: Center(
                    child: Text(
                      '消息',
                      style: TextStyle(
                        fontSize: 16,
                        color: _currentIndex == 2
                            ? Colors.black
                            : Colors.grey,
                        fontWeight: _currentIndex == 2
                            ? FontWeight.bold
                            : FontWeight.normal,
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: InkWell(
                  onTap: () => _onTabTapped(3),
                  child: Center(
                    child: Text(
                      '我',
                      style: TextStyle(
                        fontSize: 16,
                        color: _currentIndex == 3
                            ? Colors.black
                            : Colors.grey,
                        fontWeight: _currentIndex == 3
                            ? FontWeight.bold
                            : FontWeight.normal,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
